<?php

/* Reminder: always indent with 4 spaces (no tabs). */

define ('THIS_SCRIPT', 'convertdata_city.php');


require_once('../../../../lib-common.php');


function fncexec ()
{
    global $_TABLES;
	
	
    $tbl1 .= "locationstaff_data_cities";
    $tbl2 .= $_TABLES['DATABOX_base'];
	
	$tbl10 .= "wk_databox_category";
	
    $retval = '';


    $sql = "SELECT ";
    $sql .= " t1.*";
    $sql .= " ,t2.id";

    $sql .= " FROM ";
    $sql .= $tbl1." AS t1 ";
    $sql .= ", ".$tbl2." AS t2 ";
    $sql .= " WHERE ";
	$sql .= " t1.nick = t2.code";
	$sql .= " order by t1.nick ";
	
    $result = DB_query ($sql);
    $numrows = DB_numRows ($result);

    if ($numrows > 0) {
        for ($i = 0; $i < $numrows; $i++) {
            $A = DB_fetchArray ($result);
			for ($j = 1; $j < 141; $j++) {
				$cityid=$j;
				if ($A[$j]==1) {
					$id=$A['id'];
					$category_id=DB_getItem( "wk_cities"   ,"category_id","cityidnumber={$cityid}");
					echo $A['nick']." id=".$id." category_id=".$category_id."<br>";
                    $sql2="INSERT INTO {$tbl10} (";
                    $sql2.="id, category_id";
                    $sql2.=") VALUES (";
                    $sql2.="{$id}, {$category_id}";
                    $sql2.=")";
					DB_query($sql2);
				}
			}
		}

    }

    return $retval;
}


// +---------------------------------------------------------------------------+
// | MAIN                                                                      |
// +---------------------------------------------------------------------------+
function fncDisply()
{
    $retval = "";
    $retval .= "<!DOCTYPE    HTML PUBLIC '-//W3C//DTD HTML   4.01 Transitional//EN'>".LB;
    $retval .= "<html>".LB;
    $retval .= "<head>".LB;
    $retval .= "    <title>DataBox convert data city </title>".LB;
    $retval .= "</head>".LB;
    $retval .= "<body   bgcolor='#ffffff'>".LB;
    $retval .= "<h1>DataBox convert data city </h1>".LB;
    $retval .= "<form action="."'".THIS_SCRIPT."'"."method='post'>".LB;
    $retval .= "    <input type='submit' name='action' value='submit'>".LB;
    $retval .= "    <input type='submit' name='action' value='cancel'>".LB;
    $retval .= "</form>".LB;
    $retval .= "</body>".LB;
    $retval .= "</html>".LB;

    return $retval ;

}


$action ="";
if (isset ($_REQUEST['action'])) {
    $action = COM_applyFilter($_REQUEST['action'],false);
}

$display="";
if  ($action ==""){
    $display=fncDisply();
}elseif ($action =="submit"){
    $display=fncexec();
}else{
    $display ="cancel!";
}
echo $display;


?>
